package cn.itcast.controller.cargo;


import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.domain.cargo.ShippingOrderExample;
import cn.itcast.domain.company.Company;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.company.CompanyService;
import cn.itcast.utils.commons.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/cargo/shipping")
public class ShippingOrderController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private PackingService packingService;
    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list", name = "到委托页面")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        shippingOrderExample.setOrderByClause("create_time desc");
        PageInfo page = shippingOrderService.findAll(shippingOrderExample, pageNum, pageSize);
        System.out.println("page = " + page);
        request.setAttribute("page", page);
        return "cargo/shippingOrder/shippingOrder-list";
    }

    @RequestMapping(value = "/toUpdate", name = "到编辑页面")
    public String toUpdate(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        return "cargo/shippingOrder/shippingOrder-update";
    }

    @RequestMapping(value = "/updateShipping", name = "编辑委托单")
    public String updateShipping(ShippingOrder shippingOrder) {
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }


    @RequestMapping(value = "/toAdd", name = "到委托单的添加页面")
    public String toAdd(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andStateEqualTo(1l);
        PageInfo page = packingService.findAll(packingExample, pageNum, pageSize);
        request.setAttribute("page", page);
        return "cargo/shippingOrder/shippingOrder-add";
    }

    @RequestMapping(value = "/addShippering", name = "添加委托单")
    public String addShippering(ShippingOrder shippingOrder,String shippingOrderId) {
        shippingOrder.setShippingOrderId(shippingOrderId);
        shippingOrderService.save(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除委托单")
    public String delete(String id) {
        shippingOrderService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交")
    public String submit(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        shippingOrder.setState(1);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消")
    public String cancel(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        shippingOrder.setState(0);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/toView", name = "查看委托单的详细信息")
    public String toView(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        return "cargo/shippingOrder/shippingOrder-view";
    }
    @RequestMapping(value ="/gangkou",name = "查看地图")
    public String gangkou(String id){
        HashMap<String, Object> map = new HashMap<>();
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        String portOfLoading = shippingOrder.getPortOfLoading();//装船港
        String portOfDischarge = shippingOrder.getPortOfDischarge();  //卸船港
        request.setAttribute("portOfLoading",portOfLoading);
        request.setAttribute("portOfDischarge",portOfDischarge);
        return "forward:/map/map4.html";
    }
}
